let http = require("http")
let server = http.createServer(request);

server.listen(8080)
const lanPack = {
    en: {
        title: 'welcome'
    },
    zh: {
        title: '欢迎光临'
    }
}


function request(req, res){
    // Accept-Language:zh-CN, zh;q=0.9
    let acceptLanguage = req.headers['accept-language'];
    if(acceptLanguage){
        let lans = acceptLanguage.split(',').map(function(item){
            let values = item.split(';')
            let lan = values[0]
            let q = values[1] ? parseFloat(values[1].split('=')[1]) : 1;
            return {
                lan, q
            }
        }).sort((a,b)=> b.q - a.q);
        console.log(lans)
        let lan = 'en' // 默认语言
        for(let i=0; i<lans.length; i++){
            if(lanPack[lans[i].lan]){
                lan = lans[i].lan;
                break;
            }
        }
        console.log(lan)
        res.end(lanPack[lan].title)

    }
}




